package com.model.report;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.TableGenerator;

import org.hibernate.SQLQuery;
import org.hibernate.mapping.PersistentClass;

import com.core.managers.SessionFactoryManager;
import com.core.protocols.IKVObject;
import com.core.utils.LocalizableString;
import com.model.KeyValue;

@Entity
public class ReportColumn
        implements IKVObject {


	private String  name;
	private String  description;
	private String  entityColumnName;
	private String  entityAlias;
	private String  entityClassName;
	private String  joins;
	private Integer id;



	@Id
	@TableGenerator(allocationSize = 10, table = "Generators", pkColumnName = "generatorName", pkColumnValue = "generatorReportColumn", name = "generatorReportColumn")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "generatorReportColumn")
	public Integer getId () {

		return id;
	}



	public void setId ( Integer id ) {

		this.id = id;
	}



	public String getEntityClassName () {

		return this.entityClassName;
	}



	public void setEntityClassName ( String entityClassName ) {

		this.entityClassName = entityClassName;
	}



	public String getJoins () {

		return joins;
	}



	public void setJoins ( String joins ) {

		this.joins = joins;
	}



	public String getEntityAlias () {

		return entityAlias;
	}



	public void setEntityAlias ( String alias ) {

		this.entityAlias = alias;
	}



	public String getName () {

		return name;
	}



	public void setName ( String name ) {

		this.name = name;
	}



	@Lob
	public String getDescription () {

		return description;
	}



	public void setDescription ( String description ) {

		this.description = description;
	}



	public String getEntityColumnName () {

		return entityColumnName;
	}



	public void setEntityColumnName ( String entityColumnName ) {

		this.entityColumnName = entityColumnName;
	}



	@Override
	public void assignTo ( IKVObject object ) {

		if ( object == null ) {
			return;
		}

		ReportColumn objectToUpdate = (ReportColumn) object;
		objectToUpdate.name = this.name;
		objectToUpdate.description = this.description;
		objectToUpdate.entityAlias = this.entityAlias;
		objectToUpdate.joins = this.joins;
		objectToUpdate.entityClassName = this.entityClassName;
		objectToUpdate.entityColumnName = this.entityColumnName;
	}



	@Override
	public String fieldNameAsId () {

		return "id";
	}



	@Override
	public KeyValue idValue () {

		String strIdValue = this.getId().toString();
		return new KeyValue(this.fieldNameAsId(), strIdValue, false);
	}



	@Override
	public String fieldNameAsName () {

		return "name";
	}



	@Override
	public KeyValue nameValue () {

		String strIdValue = this.getName().toString();
		return new KeyValue(this.fieldNameAsName(), strIdValue, false);
	}



	@Override
	public String localizableDescription () {

		return LocalizableString.sharedInstance().localizable("entity.ReportColumn.description");
	}



	public void penetrateIntoQuery ( SQLQuery query ) {

		if ( query == null ) {
			return;
		}

		PersistentClass clazz = SessionFactoryManager.sharedInstance().annotatedClassByName(this.entityClassName);
		if ( clazz == null ) {
			return;
		}

		query.addEntity(this.entityAlias, this.entityClassName);
		query.addJoin("ASSIGNEE", "ProjectTask.assignee");
	}


}
